/*
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 *
 * Copyright @2008 the original author or authors.
 */
package com.bluestone.core;

import java.awt.event.KeyEvent;

import com.bluestone.util.Util;

/**
 * Move to an element in the page and do some actions.
 * If the element is the type of the Combox ,List or Radio, the value is the index of the values in the Combox,List or Radio.
 * @author <a href="mailto:bluestone.daniel.q@gmail.com">daniel.q</a>
 */
public class FocusAction extends Action {

	/**
	 * 
	 * @param type
	 * @param target String that defines the focus element
	 * @param value
	 * @param delay
	 * @param offset String that defines the offset relative to the upper element.
	 */
	public FocusAction(String type, String target, String value, String delay,
			String offset) {
		super(type, target, value, delay, offset);
	}

	@Override
	public boolean execute() {
		if (!super.execute()) {
			return false;
		}
		try {
			Element element = null;
			Page page = null;

			// check target , return false if null
			if (target == null || target.length() == 0)
				return false;

			// get element and page from module file
			element = Util.getCurrentProject().searchElement(target);
			if (element == null) {
				Util.error(Util.ERROR1 + target);
				return false;
			}

			// return false if element does not define the focus index value
			if (element.getIndex() < 0)
				return false;

			page = (Page) element.getParent();
			if (page == null) {
				Util.error(Util.ERROR1
						+ target.substring(0, target.lastIndexOf("|")));
				return false;
			}

			String indexPos = "";
			String topblankpos = Util.getCurrentProject().getBrowser()
					.getTopBlankPos();
			String x = topblankpos.split(",")[0];
			String y = topblankpos.split(",")[1];

			if ("TRUE".equalsIgnoreCase(page.getBrowserBar())) {
				indexPos = x
						+ ","
						+ (Util.getCurrentProject().getBarHeight() + Integer
								.parseInt(y));
			} else {
				indexPos = x
						+ ","
						+ (Util.getCurrentProject().getTitlebarHeight() + Integer
								.parseInt(y));
			}
			int count = element.getIndex();
			if (offset != null && offset.length() > 0) {
				count = count + Integer.parseInt(offset);
			}

			if (count <= 0) {
				return true;
			}

			// move to start focus position in web page
			ActionFactory.createAction(ActionFactory.MOVEACTION,
					Attributes.SCREEN, indexPos, null, null).execute();
			ActionFactory.createAction(ActionFactory.CLICKACTION, null, null,
					null, null).execute();
			robot.delay(Util.getDelayTime(Util.DELAY200));
			ActionFactory.createAction(ActionFactory.INPUTKEYACTION,
					InputKeyAction.HOMEKEY, null, null, null).execute();
			robot.delay(Util.getDelayTime(Util.DELAY200));
			ActionFactory.createAction(ActionFactory.MOVEACTION,
					Attributes.SCREEN, indexPos, null, null).execute();
			ActionFactory.createAction(ActionFactory.CLICKACTION, null, null,
					null, null).execute();

			robot.delay(Util.getDelayTime(Util.DELAY200));

			for (int i = 0; i < count; i++) {
				robot.keyPress(KeyEvent.VK_TAB);
				robot.delay(Util.getDelayTime(Util.DELAY200));
				robot.keyRelease(KeyEvent.VK_TAB);
				robot.delay(Util.getDelayTime(Util.DELAY300));
			}

			robot.delay(Util.getDelayTime(Util.DELAY500));
			String type = element.getType();
			String btype = Util.getCurrentProject().getBrowser().getType();
			if (Attributes.TYPE_BUTTON.equalsIgnoreCase(type)
					|| Attributes.TYPE_CHECKBOX.equalsIgnoreCase(type)) {
				ActionFactory.createAction(ActionFactory.INPUTKEYACTION,
						InputKeyAction.SPACEKEY, null, null, null).execute();
			} else if (Attributes.TYPE_LIST.equalsIgnoreCase(type)) {

				if (Browser.FIREFOX2.equalsIgnoreCase(btype)
						|| Browser.FIREFOX3.equalsIgnoreCase(btype)) {
					ActionFactory.createAction(ActionFactory.INPUTKEYACTION,
							InputKeyAction.DOWNKEY, null, null, null).execute();
				} else {
					ActionFactory.createAction(ActionFactory.INPUTKEYACTION,
							InputKeyAction.SPACEKEY, null, null, null)
							.execute();
				}
				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionFactory.createAction(
								ActionFactory.INPUTKEYACTION,
								InputKeyAction.DOWNKEY, null,
								"" + Util.getDelayTime(Util.DELAY200), null)
								.execute();
					}
				}
			} else if (Attributes.TYPE_LINK.equalsIgnoreCase(type)
					|| Attributes.TYPE_TAB.equalsIgnoreCase(type)) {
				ActionFactory.createAction(ActionFactory.INPUTKEYACTION,
						InputKeyAction.ENTERKEY, null, null, null).execute();
			} else if (Attributes.TYPE_COMBOX.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionFactory.createAction(
								ActionFactory.INPUTKEYACTION,
								InputKeyAction.DOWNKEY, null,
								"" + Util.getDelayTime(Util.DELAY200), null)
								.execute();
					}
					if (Browser.FIREFOX2.equalsIgnoreCase(btype)
							|| Browser.FIREFOX3.equalsIgnoreCase(btype)) {
						ActionFactory.createAction(
								ActionFactory.INPUTKEYACTION,
								InputKeyAction.ENTERKEY, null, null, null)
								.execute();
					}

				}
			} else if (Attributes.TYPE_RADIO.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					int pos = Integer.parseInt(value);
					for (int i = 0; i < pos - 1; i++) {
						ActionFactory.createAction(
								ActionFactory.INPUTKEYACTION,
								InputKeyAction.DOWNKEY, null,
								"" + Util.getDelayTime(Util.DELAY200), null)
								.execute();
					}
				}

			} else if (Attributes.TYPE_TEXTFIELD.equalsIgnoreCase(type)) {
				if (value != null && value.length() > 0) {
					ActionFactory.createAction(ActionFactory.INPUTTEXTACTION,
							null, value, null, null).execute();
				}
			}

			if (delay != null && delay.length() > 0) {
				robot.delay(Util.getDelayTime(Integer.parseInt(delay)));
			} else {
				robot.delay(Util.getDelayTime(Util.FOCUSDELAY));
			}

		} catch (Exception me) {
			me.printStackTrace();
			Util.error(me);
			return false;
		}
		return true;
	}

}
